package com.spring.component.easyexcel.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

/**
 * 读取excel监听事件，继承AnalysisEventListener，泛型为收集每列数据对象，这里用Map收集，key为列序号，value为每列值
 * easyExcel不指定收集器时默认为LinkedHashMap
 */
@Slf4j
public class NoModelDataListener extends AnalysisEventListener<Map<Integer, String>> {

  @Override
  public void invoke(Map<Integer, String> stringStringMap, AnalysisContext analysisContext) {
    int row = analysisContext.readRowHolder().getRowIndex();
    int sheetNo = analysisContext.readSheetHolder().getSheetNo();
    String sheetName = analysisContext.readSheetHolder().getSheetName();
    log.info("读取到[{}]的第{}行记录：{}", sheetName, row, JSONObject.toJSONString(stringStringMap));
  }

  @Override
  public void doAfterAllAnalysed(AnalysisContext analysisContext) {
    log.info("excel数据读取完成");
  }

}
